package starlingui.component
{
	import com.imcrl.component.itemRender.IListItemRender;
	import com.imcrl.core.imcrl_internal;
	import com.imcrl.effect.ShakeEffect;
	import com.imcrl.layout.TileLayout;
	
	import starlingui.component.itemRender.IconItemRender;
	
	public class AdvLayout extends TileLayout
	{
		private var list:Vector.<IconItemRender>;
		public function AdvLayout(width:int, height:int, col:int)
		{
			super(width, height, col);
			
			list=new Vector.<IconItemRender>();
			
			
			pad.left=width>>1;
			pad.top=height>>1;
		}
		
		
		private var effect:ShakeEffect=new ShakeEffect(2);
		public function shakeAll(bool:Boolean=true):void{
			var icon:IconItemRender;
			effect.clearAndStop();
			if(bool){
				for each(icon in list){
					effect.add(icon);
				}
				effect.start();		
			}
		}
		
		
		public function removeItem(value:IconItemRender):Boolean{
			var index:int=list.indexOf(value);
			if(index==-1){
				return false;
			}
			
			effect.remove(value);
			
			list.splice(index,1);
			var viewList:AdvPageList=value.parent as AdvPageList;
			if(viewList){
				viewList.imcrl_internal::removeItem(value,true);
			}
			var len:int=list.length;
			for(var i:int=index;i<len;i++){
				value=list[i];
				value.index-=1;
				
				tp=getIndexPosition(value.index);
				
				if(tp.x==value.x && tp.y==value.y)continue;
				
				value.tweenPropertys(["x","y"],[tp.x,tp.y]);
			}
			
			
			return true;
		}
		
		/**
		 * 
		 * @param dragIndex
		 * @param dropIndex
		 * @return 
		 * 
		 */		
		public function changeSlot(dragIndex:int,dropIndex:int):Boolean{
			if(dragIndex==dropIndex)return false;
			
			var dragItem:IconItemRender=list[dragIndex];
			var dropItem:IconItemRender=list[dropIndex];
			var dx:int=dropIndex-dragIndex;
			
			
			dragItem.index=dropItem.index;
			list.splice(dragIndex,1);
			
			
			var todoList:Vector.<IconItemRender>;
			var item:IconItemRender;
			var t:int;
			var i:int;
			var x0:int;
			var y0:int;
			if(dx>0){
				todoList=list.splice(dragIndex,dx);
				
				for(i=0;i<dx;i++){
					item=todoList[i];
					item.index-=1;
					list.splice(dragIndex+i,0,item);
					tp=getIndexPosition(item.index)
					item.tweenPropertys(["x","y"],[tp.x,tp.y]);
				}
				
				list.splice(dropIndex,0,dragItem);
				
				return true;
			}
			
			//todo 向前拉图标;
			dx=Math.abs(dx);
			todoList=list.splice(dropIndex,dx);
			for(i=0;i<dx;i++){
				item=todoList[i];
				item.index+=1;
				list.splice(dropIndex+i,0,item);
				tp=getIndexPosition(item.index);
				item.tweenPropertys(["x","y"],[tp.x,tp.y]);
			}
			
			list.splice(dropIndex,0,dragItem);
			
			return true;
		}
		
		
		override public function layout(item:IListItemRender,index:int):void{
			super.layout(item,index);
			list.push(item);
		}
		
		
		override public function clear():void{
			list.length=0;
		}
		
		public function getList():Vector.<IconItemRender>{
			return list;
		}
		
		/**
		 * 合并; 
		 * 
		 */		
		public function unExpand():void{
			var cx:int=Application.WIDTH>>1;
			var cy:int=Application.HEIGHT>>1;
			
			var dx:int;
			var dy:int;
			var degress:Number;
			var len:int;
			var maxLen:int=700;
			for each(var icon:IconItemRender in list){
				tp=getIndexPosition(icon.index);
				dx=tp.x-cx;
				dy=tp.y-cy;
				
				degress=Math.atan2(dy,dx);
				
				len=Math.sqrt(dx*dx+dy*dy);
				
				//icon.debug(len+","+int(degress*180/Math.PI));
				
				len=maxLen-len;
				
				icon.x=tp.x+len*Math.cos(degress);
				icon.y=tp.y+len*Math.sin(degress);
				icon.alpha=0;
				
				icon.tweenPropertys(["x","y","alpha"],[tp.x,tp.y,1],.3);
			}
		}
		
		
		/**
		 * 展开
		 * 
		 */		
		public function expand():void{
			var cx:int=Application.WIDTH>>1;
			var cy:int=Application.HEIGHT>>1;
			
			var dx:int;
			var dy:int;
			var degress:Number;
			var len:int;
			var maxLen:int=600;
			for each(var icon:IconItemRender in list){
				tp=getIndexPosition(icon.index);
				dx=tp.x-cx;
				dy=tp.y-cy;
				
				degress=Math.atan2(dy,dx);
				
				len=Math.sqrt(dx*dx+dy*dy);
				
				//icon.debug(len+","+int(degress*180/Math.PI));
				
				len=maxLen-len;
				dx=tp.x+len*Math.cos(degress);
				dy=tp.y+len*Math.sin(degress);
				
				icon.tweenPropertys(["x","y","alpha"],[dx,dy,0],.3);
			}
		}
		
		
		/**
		 * 
		 * @param item
		 * @param top
		 * @param botton
		 * 
		 */		
		public function expandBy(itemRender:IconItemRender,top:int,botton:int,alpha:Number,touchable:Boolean):void{
			var item:IconItemRender;
			var clickRow:int=Math.floor(itemRender.index/col); 
			var row:int;
			var values:Array=[];
			for each(item in list){
				row=Math.floor(item.index/col);
				item.touchable=touchable;
				
				tp=getIndexPosition(item.index);
				if(row<=clickRow){
					values[0]=tp.y+top;
					if(item==itemRender){
						values[1]=1;
					}else{
						values[1]=alpha;
					}
				}else{
					values[0]=tp.y+botton;
					values[1]=alpha;
				}
				
				item.tweenPropertys(["y","alpha"],values);
			}
		}
		
	}
}